//
//  GDTabBar.m
//  GDWeiboNJ
//
//  Created by zhouguodong on 15/4/3.
//  Copyright (c) 2015年 shuanyuan. All rights reserved.
//

#import "GDTabBar.h"
#import "UIView+Frame.h"
@interface GDTabBar()
/**
 *  加号按钮
 */
@property (nonatomic, weak)  UIButton *plusBtn;
@end
@implementation GDTabBar
// 通过代码创建
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setup];
    }
    return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    {
        if (self) {
            [self setup];
        }
    }
    return self;
}

// 创建按钮
- (void)setup
{
    // 创建一个加号按钮, 添加到当前控件中
    // 1.创建按钮
    UIButton *plusBtn = [[UIButton alloc] init];
    plusBtn.tag = 998;
    // 2.设置背景图片
    [plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
    [plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
    // 3.设置图标
    [plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
    [plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
    // 4.添加
    [self addSubview:plusBtn];
    // 5.绑定成员属性
    self.plusBtn = plusBtn;
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    /*测试
    for (UIView *child in self.subviews) {
        child.frame = CGRectMake(0, 0, 100, 100);
    }
     */
    
    NSUInteger index = 0;
    
    for (UIView *child in self.subviews) {
       // 1.判断是否是UITabbarButton
        // UITabbarButton是系统的私有控件
        if ([child isKindOfClass:[UIControl class]]&&![child isKindOfClass:[UIButton class]]){
            // 另一种判断方式,可以将系统自带的类转化为私有的类,来进行判断
//            if ([child isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
          
            
            // 2.计算每个按钮的frame
            
            CGFloat childW = self.frame.size.width/5;
            CGFloat childH = self.frame.size.height;
            CGFloat childX = index *childW;
            CGFloat childY = 0;
            
            // 判断是否是中间的按钮
            if(index == 2)
            {
                index++;
                childX = index*childW;
            }
            index++;
            child.frame = CGRectMake(childX, childY, childW, childH);
                
        }
        // 设置加号按钮的frame
        self.plusBtn.size = self.plusBtn.currentBackgroundImage.size;
        self.plusBtn.centerX = self.width * 0.5;
        self.plusBtn.centerY = self.height * 0.5;
    }
}


@end
